package com.taotao.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.taotao.common.enums.OperateType;
import com.taotao.common.pojo.BaseResult;
import com.taotao.common.utils.Constants;
import com.taotao.common.utils.DateUtil;
import com.taotao.mapper.EyAddressMapper;
import com.taotao.mapper.EyMemberMapper;
import com.taotao.pojo.EyAddress;
import com.taotao.pojo.EyAddressExample;
import com.taotao.pojo.EyMember;
import com.taotao.service.AddressService;
import com.taotao.service.OperateLogService;
import com.taotao.service.WechatService;

@Service
public class AddressServiceImpl implements AddressService {

	@Autowired
	EyAddressMapper eyAddressMapper;
	@Autowired
	WechatService wechatService;
	@Autowired
	EyMemberMapper eyMemberMapper;
	@Autowired
	OperateLogService operateLogService;
	
	BaseResult baseResult = new BaseResult();
	@Override
	public Map<String, Object> addressAdd(Map<String, String> map) {
		try {
			String memberId=map.get("memberId");
			String contactName = map.get("contactName");
			String mobile = map.get("mobile");
			String centerData = map.get("centerData");
			String serviceAddress = map.get("serviceAddress");
			String detailAddress = map.get("detailAddress");
			
			if(!wechatService.msgCheck(detailAddress)){
				return baseResult.getErrorMap("The detailAddress field is risky");
			}
			
			EyMember member = eyMemberMapper.selectByPrimaryKey(Integer.parseInt(memberId));
			if(member==null){
				return baseResult.getErrorMap("会员编号不存在,memberId:"+memberId);
			} 
			
			EyAddress address = new EyAddress();
			address.setMemberId(Integer.parseInt(memberId));
			address.setContactName(contactName);
			address.setMobile(mobile);
			address.setCenterData(centerData);
			address.setDetailAddress(detailAddress);
			address.setServiceAddress(serviceAddress);
			address.setStatus(Constants.num_1);
			address.setCreateTime(DateUtil.getDateAndTime());
			address.setUpdateTime(DateUtil.getDateAndTime());
			eyAddressMapper.insert(address);
			
			Map<String,Object> m=new HashMap<>();
			m.put("address", address);
			operateLogService.insert(Integer.parseInt(memberId), null,null,OperateType.operateType_31.getDesc(),OperateType.operateType_31.getCode(),""); 
			
			return baseResult.getSuccMap(m);
			
		} catch (Exception e) {
			e.printStackTrace();
			return baseResult.getErrorMap("网络繁忙，请稍后再试");
		}
	}
	
	
	@Override
	public Map<String, Object> getAddressList(Map<String, String> map) {
		try {
			String memberId=map.get("memberId");
			List<EyAddress> list=null;
			if(StringUtils.isNotBlank(memberId)){
				EyAddressExample example = new EyAddressExample();
				com.taotao.pojo.EyAddressExample.Criteria criteria = example.createCriteria();
				criteria.andMemberIdEqualTo(Integer.parseInt(memberId));
				criteria.andStatusEqualTo(Constants.num_1); //正常
				example.setOrderByClause(" update_time desc ");
				list = eyAddressMapper.selectByExample(example);
			}
		
			return baseResult.getSuccMap(list);
		} catch (Exception e) {
			e.printStackTrace();
			return baseResult.getErrorMap("网络繁忙，请稍后再试");
		}
	}
	
	
	@Override
	public Map<String, Object> getAddressInfo(Map<String, String> map) {
		try {
			String addressId=map.get("addressId");
			EyAddress address = eyAddressMapper.selectByPrimaryKey(Integer.parseInt(addressId));
			return baseResult.getSuccMap(address);
		} catch (Exception e) {
			e.printStackTrace();
			return baseResult.getErrorMap("网络繁忙，请稍后再试");
		}
	}
	
	@Override
	public Map<String, Object> deleteAddress(Map<String, String> map) {
		try {
			String memberId=map.get("memberId");
			String addressId=map.get("addressId");
			EyAddress address = eyAddressMapper.selectByPrimaryKey(Integer.parseInt(addressId));
			address.setStatus(Constants.num_100); //刪除
			address.setUpdateTime(DateUtil.getDateAndTime());
			eyAddressMapper.updateByPrimaryKey(address);
			
			operateLogService.insert(Integer.parseInt(memberId), null,null,OperateType.operateType_33.getDesc(),OperateType.operateType_33.getCode(),""); 

			return baseResult.getSuccMap();
			
		} catch (Exception e) {
			e.printStackTrace();
			return baseResult.getErrorMap("网络繁忙，请稍后再试");
		}
	}
	
	
	@Override
	public Map<String, Object> modifyAddress(Map<String, String> map) {
		try {
			String addressId=map.get("addressId");
			String memberId=map.get("memberId");
			String contactName = map.get("contactName");
			String mobile = map.get("mobile");
			String centerData = map.get("centerData");
			String serviceAddress = map.get("serviceAddress");
			String detailAddress = map.get("detailAddress");
			
			if(!wechatService.msgCheck(detailAddress)){
				return baseResult.getErrorMap("The details field is risky");
			}
			
			EyMember member = eyMemberMapper.selectByPrimaryKey(Integer.parseInt(memberId));
			if(member==null){
				return baseResult.getErrorMap("会员编号不存在,memberId:"+memberId);
			} 
			
			EyAddress address = new EyAddress();
			address.setId(Integer.parseInt(addressId));
			address.setMemberId(Integer.parseInt(memberId));
			address.setContactName(contactName);
			address.setMobile(mobile);
			address.setCenterData(centerData);
			address.setDetailAddress(detailAddress);
			address.setServiceAddress(serviceAddress);
			address.setStatus(Constants.num_1);
			address.setUpdateTime(DateUtil.getDateAndTime());
			eyAddressMapper.updateByPrimaryKeySelective(address);
			
			operateLogService.insert(Integer.parseInt(memberId), null,null,OperateType.operateType_32.getDesc(),OperateType.operateType_32.getCode(),""); 
			
			Map<String,Object> m=new HashMap<>();
			m.put("address", address);
			return baseResult.getSuccMap(m);
			
		} catch (Exception e) {
			e.printStackTrace();
			return baseResult.getErrorMap("网络繁忙，请稍后再试");
		}
	}

}
